#ifndef CLI_TASK_H_
#define CLI_TASK_H_

#include "General_definitions.h"

#include "gpio.h"
#include "usart.h"
#include "string.h"
#include "stdio.h"
#include "FreeRTOS.h"
#include "printf-stdarg.h"
#include "task.h"
#include "malloc.h"
#include "usart_fast.h"
#include "delay.h"
#include "file_system.h"
#include "xmodem.h"
#include "Parameters.h"
#include "common_def.h"

extern xTaskHandle network_task_handle;


#define PDCA_CLOCK_HSB      AVR32_PDCA_CLK_HSB
#define PDCA_CLOCK_PB       AVR32_PDCA_CLK_PBA

// }


// COMMAND
// {
#ifdef DEBUGING
#define MAX_CMD_COUNT	25+1+1+1
#else
#define MAX_CMD_COUNT	15
#endif//DEBUGING
#define MAX_CMD_LEN		9
//#define MAX_CMD_DESC	65
//#define MAX_CMD_HLP 	70
#define MAX_PARAM_LEN	32
#define MAX_ARG_LEN		11
#define MAX_OPT_LEN		9
#define MAX_LINE_SIZE	MAX_CMD_LEN+1+MAX_PARAM_LEN+1+MAX_ARG_LEN+1+MAX_OPT_LEN+1

typedef struct
{
	int         (*Function)();
	char         *Name;//Name[MAX_CMD_LEN];
	char         *Description;//[MAX_CMD_DESC];
	char		 *Help;//[MAX_CMD_HLP];
	int			cmd_type;
}Command;

//}

// ERROR
// {

#define ERR_NONE						0
#define ERR_SYNTAX_ERROR				1
#define ERR_INVALID_COMMAND				2
#define ERR_INVALID_PARAM				3
#define ERR_VAL_OUT_OF_RANGE			4
#define ERR_SETTING_CONFLICT			5
#define ERR_READ_ONLY					6
#define ERR_MISSED_PARAM_VAL			7
#define ERR_SWITCH_TO_DATAMODE			8
#define ERR_MISSED_FILE					9
#define ERR_UNSPECIFIED					10

// }



// CLI
// {

#define USART_IF_ACTIVE    0
#define USB_IF_ACTIVE      1

//int echo_mode; //this field declared in Parameters.h
int file_trans;
//int advanced_mode;
int command_mode;
//int adv_req;
int cmd_req;

char prompt[8];
#define CLI_PROMPT prompt
#define CLI_PROMPT_NORM "CLI> "
#define CLI_PROMPT_ADV 	"ADV> "
#define CLI_PROMPT_DBG 	"DBG> "
#define CLI_PROMPT_SET_TO_NORM strcpy(CLI_PROMPT, CLI_PROMPT_NORM)
#define CLI_PROMPT_SET_TO_ADV strcpy(CLI_PROMPT, CLI_PROMPT_ADV)
#ifdef DEBUGING
#define CLI_PROMPT_SET_TO_DBG strcpy(CLI_PROMPT, CLI_PROMPT_DBG)
#endif//DEBUGING


#ifdef DEBUGING
#define DBG_CLI_COMMAND		4
#define NORM_CLI_COMMAND 5
#define ADV_CLI_COMMAND 6
#else
#define NORM_CLI_COMMAND	1
#define ADV_CLI_COMMAND		2
#endif//DEBUGING

#define ECHO_ON		echo_mode
#define ECHO_off	0
#define ECHO_on		1
#define IS_FILE		file_trans
/*
#define ADV_MOD advanced_mode
#define ADV_ON 1
#define ADV_OFF 0

#define ADV_REQ adv_req
#define ADV_REQ_START	adv_req = 1
#define ADV_REQ_END		adv_req = 0
*/
#define NORM_ON 1
#define ADV_ON 2
#ifdef DEBUGING
#define DBG_ON 4
#endif//DEBUGING

#define NORM_REQ_START	cmd_req =0
#define NORM_REQ	0
#define ADV_REQ_START	cmd_req = 1
#define ADV_REQ		1
#ifdef DEBUGING
#define DBG_REQ_START	cmd_req = 2
#define DBG_REQ		2
#endif//DEBUGING

// }



// FUNCTIONS
// {

extern unsigned char test_mode_param ;

char command[MAX_CMD_LEN];
char param[MAX_PARAM_LEN];
char arg[MAX_ARG_LEN];
char option[MAX_OPT_LEN];
char line[MAX_LINE_SIZE];
int line_ind;
char original_line[MAX_LINE_SIZE];
int original_line_ind;

void cli_op_mode_init(void);
void cli_init(void);
void cli_task_init(void);
void cli_task_delete(void);

void cli(unsigned char c);
unsigned char capital(unsigned char c);
int analys_line(void);
int execute(void);
int print_cmd_help(char* cmd_);

int cmd_CMD1(void);
int cmd_PHSH(void);
int cmd_PWRC(void);
int cmd_PDET(void);
int cmd_STAT(void);
int cmd_link(void);
int cmd_sleep(void);
int cmd_ncc(void);
int cmd_state(void);

int cmd_dport(void);
int cmd_xmod(void);
int cmd_llc_sim(void);

int cmd_WAKE(void);
int cmd_IDET(void);
int cmd_WIFI(void);

int cmd_help(void);
void PrintParamCodeIndex(const Parameter* _Param, char index, char IsPrintName);
void PrintParamInt(const Parameter* _Param);
int cmd_ati_info(void);
int cmd_echo(void);
int cmd_get(void);
int cmd_set(void);
inline int cmd_save(void);
#ifdef DEBUGING
int cmd_dbg(void);
#endif//DEBUGING
int cmd_test(void);
int cmd_read(void);
int cmd_write(void);
int cmd_reboot(void);
int cmd_datamode(void);
int cmd_exit(void);
int cmd_dump(int, int);
int cmd_dump_read(int, unsigned char*, int);
int execute_line(const char* in_line);
// }
void exit_to_data_mode(void);
char active_interface(void);

extern xTaskHandle cli_task_handle;

#endif

